package com.twu.biblioteca;

/**
 * twu-biblioteca-ziyiliu
 * User: Ziyi.LIU
 * Date: 14-6-19
 */
public class User {

    private String username;
    private String encryptedPassword;    //Simple SHA-1 password encryption with no salt
    private String email;
    private String phone;

    public User(String username, String encryptedPassword, String email, String phone) {
        this.username = username;
        this.encryptedPassword = encryptedPassword;
        this.email = email;
        this.phone = phone;
    }

    public String getUsername() {
        return username;
    }

    public String getEncryptedPassword() {
        return encryptedPassword;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    @Override
    public String toString() {
        return "Username: " + getUsername() + "\n" +
               "Email: " + getEmail() + "\n" +
               "Phone: " + getPhone() + "\n";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (email != null ? !email.equals(user.email) : user.email != null) return false;
        if (!encryptedPassword.equals(user.encryptedPassword)) return false;
        if (phone != null ? !phone.equals(user.phone) : user.phone != null) return false;
        if (!username.equals(user.username)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = username.hashCode();
        result = 31 * result + encryptedPassword.hashCode();
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phone != null ? phone.hashCode() : 0);
        return result;
    }

    public Material checkoutMaterial(int materialId) throws MaterialCheckOutException {
        return Library.INSTANCE.checkoutMaterial(materialId, this);
    }

    public boolean returnMaterial(Material material) throws MaterialReturnException {
        return Library.INSTANCE.returnMaterial(material, this);
    }

    public boolean returnMaterial(int materialId) throws MaterialReturnException {
        return Library.INSTANCE.returnMaterial(materialId, this);
    }
}
